package leetcode.f1t100;

/**
 * 输出数组最大的子串和
 * https://leetcode.com/problems/maximum-subarray/
 *
 * @Author lichongxiao
 * @Date 2022/10/25 9:09
 */
public class Q53_MaximumSubarray {
    public int maxSubArray(int[] nums) {
        int cur = 0,max = Integer.MIN_VALUE;
        for(int num :nums){
            cur += num;
            max = max > cur ? max : cur;
            cur = cur < 0 ? 0 : cur;
        }
        return max;
    }

    public static void main(String[] args) {
        Q53_MaximumSubarray bean = new Q53_MaximumSubarray();
        int max = bean.maxSubArray(new int[]{1, -1, 1, -1});
        System.out.println(max);
    }
}
